﻿using Common.Device.Enums;
using Common.Device.Interfaces;
using Common.Device.ViewModels;
using Common.Tool.DataTools;
using Common.Tool.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Device.Models
{
    public class DianYuan : Device
    {
        /// <summary>
        /// 测试模式
        /// </summary>
        public DianYuanEnum.TestMode mode { get; set; }
        public int 通道 { get; set; }
        public double 输出电压 { get; set; }
        public double? 输出电流 { get; set; }
        /// <summary>
        /// 获取测试指令
        /// </summary>
        /// <param name="deviceName">仪器名称</param>
        /// <returns></returns>
        public override Order GetOrder(string deviceName)
        {
            //测试指令
            Order order = new Order();
            order.temp = mode.ToString();
            order.name = deviceName;
            switch (mode)
            {
                case DianYuanEnum.TestMode.设置输出:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, 输出电压, 输出电流 });
                    break;
                case DianYuanEnum.TestMode.读取电压:
                    order.range = GetRange("V");
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, 上限值, 下限值 });
                    break;
                case DianYuanEnum.TestMode.读取电流:
                    order.range = GetRange("A");
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, 上限值, 下限值 });
                    break;
                case DianYuanEnum.TestMode.读取功率:
                    order.range = GetRange("W");
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, 上限值, 下限值 });
                    break;
                case DianYuanEnum.TestMode.关闭输出:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道});
                    break;
                default:
                    break;
            }
            return order;
        }
        /// <summary>
        /// 测试并返回结果
        /// </summary>
        /// <param name="dev">仪器实体</param>
        /// <param name="testOrder">测试项指令</param>
        /// <returns></returns>
        public override DeviceResult GetResult(object dev, TestOrder testOrder)
        {
            var device = dev as IDianYuan;
            var result = new DeviceResult();
            switch (mode)
            {
                case DianYuanEnum.TestMode.设置输出:
                    //设置输出
                    //输出电压，支持循环函数功能，通过循环次数计算
                    var vol = (decimal)输出电压;
                    if (testOrder != null && !string.IsNullOrEmpty(testOrder.loopName))
                    {
                        vol += (decimal)(device.Name == testOrder.loopName ? testOrder.loopNum * testOrder.loopTime : 0);
                    }
                    var r1 = device.SetOn((double)vol, 输出电流 ?? 0, 通道);
                    result.success = r1.IsSucceed;
                    result.result = r1.IsSucceed ? "OK" : r1.Err;
                    break;
                case DianYuanEnum.TestMode.读取电压:
                    //读取电压
                    var r2 = device.GetVoltage(通道);
                    //测试结果赋值
                    result.success = r2.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r2.Err;
                        break;
                    }
                    result.value = r2.Value;
                    result.unit = "V";
                    result.result = DataConvert.ToUnit(r2.Value, "F3") + result.unit;
                    //判断是否在范围内
                    if (下限值 != null && result.success)
                    {
                        result.success = r2.Value >= 下限值;
                    }
                    if (上限值 != null && result.success)
                    {
                        result.success = r2.Value <= 上限值;
                    }
                    break;
                case DianYuanEnum.TestMode.读取电流:
                    //读取电流
                    var r3 = device.GetCurrent(通道);
                    //测试结果赋值
                    result.success = r3.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r3.Err;
                        break;
                    }
                    result.value = r3.Value;
                    result.unit = "A";
                    result.result = DataConvert.ToUnit(r3.Value, "F3") + result.unit;
                    //判断是否在范围内
                    if (下限值 != null && result.success)
                    {
                        result.success = r3.Value >= 下限值;
                    }
                    if (上限值 != null && result.success)
                    {
                        result.success = r3.Value <= 上限值;
                    }
                    break;
                case DianYuanEnum.TestMode.读取功率:
                    //读取功率
                    var r4 = device.GetPower(通道);
                    //测试结果赋值
                    result.success = r4.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r4.Err;
                        break;
                    }
                    result.value = r4.Value;
                    result.unit = "W";
                    result.result = DataConvert.ToUnit(r4.Value, "F3") + result.unit;
                    //判断是否在范围内
                    if (下限值 != null && result.success)
                    {
                        result.success = r4.Value >= 下限值;
                    }
                    if (上限值 != null && result.success)
                    {
                        result.success = r4.Value <= 上限值;
                    }
                    break;
                case DianYuanEnum.TestMode.关闭输出:
                    //关闭输出
                    var r5 = device.SetOff(通道);
                    //测试结果赋值
                    result.success = r5.IsSucceed;
                    result.result = r5.IsSucceed ? "OK" : r5.Err;
                    break;
                default:
                    break;
            }
            return result;
        }
    }
}
